<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <!-- 导入依赖 -->
    <script src="https://lib.baomitu.com/leaflet/1.9.2/leaflet.js"></script>
    <link rel="stylesheet" href="https://lib.baomitu.com/leaflet/1.9.2/leaflet.css">
    <style>
        * {
            margin: 0;
            padding: 0
        }

        #map {
            width: 100vw;
            height: 100vh;
        }
    </style>
</head>

<body>
    <button onclick="clearG()">清除标祝</button>
    <div id="map">

    </div>
    <script>
        // 思路：每点击一次，就创建一个坐标为当前坐标点的geojson点数据，
        // 并添加到geojson图层中。
        /* 瓦片图层 */
        var url = 'https://api.mapbox.com/styles/v1/mapbox/streets-v11/tiles/{z}/{x}/{y}?access_token=pk.eyJ1IjoiY2hlbmdiZW5jaGFvIiwiYSI6ImNsODU3aGRiODA0Y2UzcHBzZmFlcmdqZ2sifQ.8k59W_pB_Riwe6o-MneRlA'
        var layer = L.tileLayer(url, {
            maxZoom: 19,
            attribution: '© OpenStreetMap'
        })
        /* 实例化地图 */
        const map = L.map("map", {
            layers: [layer],
            /* center:[纬度,经度] */
            center: [30.50, 114.30],
            zoom: 4
        })
        /* 空的图层 */
        var geojson_layer = L.geoJSON();
        map.addLayer(geojson_layer);
        //
        map.on("click", evt => {
            var { lng, lat } = evt.latlng;
           /*  var data = {
                type: "FeatureCollection",
                features: [
                    {
                        type: "Feature",
                        geometry: {
                            type: "Point",
                            coordinates: [lng, lat]
                        },
                        properties: {
                            name: "武汉"
                        },
                        id: 10001
                    }
                ]
            } */
            var data = {
                type: "Feature",
                geometry: {
                    type: "Point",
                    coordinates: [lng, lat]
                }
            }
            // console.log(data);
            // 此处添加的是 非标准数据 Feature【不是FeatureCollection】
            // data是标准或非标准均可
            geojson_layer.addData(data);
        })
        // console.log(geojson_layer)
        function clearG() {
            // console.log(1);
            geojson_layer.clearLayers()
        }
    </script>
</body>

</html>